import http from './http'

export interface StatisticsData {
  totalAlerts: number
  alertsChange: number
  totalTickets: number
  ticketsChange: number
  mttr: number
  mttrChange: number
  slaCompliance: number
  slaChange: number
  period: {
    start: string
    end: string
  }
}

export interface TrendDataPoint {
  date: string
  alerts: number
  tickets: number
}

export interface DistributionDataPoint {
  date: string
  open: number
  inProgress: number
  resolved: number
  closed: number
}

export interface SeverityData {
  severity: string
  count: number
}

export interface ResponseTimeData {
  date: string
  avgResponseTime: number
}

export interface PerformanceData {
  avgResponseTime: number
  firstResponseSLA: number
  resolutionRate: number
  totalTickets: number
  resolvedTickets: number
}

export interface TimeRange {
  startDate?: string
  endDate?: string
}

/**
 * 获取统计数据
 */
export const getStatistics = async (params?: TimeRange) => {
  return await http.get<StatisticsData>('/analytics/statistics', { params })
}

/**
 * 获取告警趋势
 */
export const getAlertTrend = async (params?: { period?: 'week' | 'month' | 'year' }) => {
  return await http.get<TrendDataPoint[]>('/analytics/alert-trend', { params })
}

/**
 * 获取工单分布
 */
export const getTicketDistribution = async (params?: { period?: 'week' | 'month' }) => {
  return await http.get<DistributionDataPoint[]>('/analytics/ticket-distribution', { params })
}

/**
 * 获取告警严重程度统计
 */
export const getAlertSeverity = async (params?: TimeRange) => {
  return await http.get<SeverityData[]>('/analytics/alert-severity', { params })
}

/**
 * 获取响应时间统计
 */
export const getResponseTime = async (params?: { period?: 'week' | 'month' }) => {
  return await http.get<ResponseTimeData[]>('/analytics/response-time', { params })
}

/**
 * 获取性能报告
 */
export const getPerformanceReport = async (params?: TimeRange) => {
  return await http.get<PerformanceData>('/analytics/performance', { params })
}

// 保持向后兼容
export const getAnalyticsData = getStatistics
export const getTicketTrend = getTicketDistribution
export const getPerformanceMetrics = getPerformanceReport
